{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test the BiDAF runtime model\n",
    "This notebook uses the BiDAF language entitiy recognizer model.  See the [README.md](../README.md) for instructions on how to run this sample.\n",
    "\n",
    "## `model_corebot101` package\n",
    "This sample creates a python package (`model_corebot101`) which contains all the code to train, evaluate and infer intent classifier for this sample.\n",
    "\n",
    "## See also:\n",
    "- [The BERT training model](bert_train.ipynb) to train the intent classifier model.\n",
    "- [The BERT runtime model](bert_model_runtime.ipynb) to test the BERT model to test the intent classifier model.\n",
    "- [The model runtime](model_runtime.ipynb) to test the both the BERT and BiDAF model together."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from model_corebot101.bidaf.model_runtime.bidaf_model_runtime import BidafModelRuntime\n",
    "import os\n",
    "from pathlib import Path\n",
    "from IPython.display import display\n",
    "\n",
    "bidaf_model_dir = os.path.abspath(os.path.join(Path.home(), \"models/bidaf\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `BidafModelRuntime` class\n",
    "The `BidafModelRuntime` class is used to perform the classification for entities on the bot utterances.\n",
    "\n",
    "The model is completely is downloaded and placed in the `<home dir>/models/bidaf` directory.\n",
    "\n",
    "## `BidafModelRuntime.init_bidaf` method\n",
    "The `BidafModelRuntime.init_bidaf` method downloads the necessary ONNX model.\n",
    "\n",
    "Output should look like the following: \n",
    "\n",
    "```bash\n",
    "Creating bidaf model directory..\n",
    "Checking file ../../bot/cognitiveModels/bidaf\\bidaf.onnx..\n",
    "Downloading bidaf.onnx...\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "bidaf model directory already present..\n",
      "[nltk_data] Downloading package punkt to\n",
      "[nltk_data]     C:\\Users\\daveta\\models\\bidaf...\n",
      "[nltk_data]   Package punkt is already up-to-date!\n",
      "[nltk_data] Downloading package punkt to\n",
      "[nltk_data]     C:\\Users\\daveta\\AppData\\Roaming\\nltk_data...\n",
      "[nltk_data]   Package punkt is already up-to-date!\n",
      "Checking file C:\\Users\\daveta\\models\\bidaf\\bidaf.onnx..\n",
      "bidaf.onnx downloaded already!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'The BiDAF model successfully downloaded.'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "if not BidafModelRuntime.init_bidaf(bidaf_model_dir, True):\n",
    "    display('The BiDAF model was not downloaded successfully.  See output below for more details.')\n",
    "else:\n",
    "    display('The BiDAF model successfully downloaded.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `BidafModelRuntime` class\n",
    "The `BidafModelRuntime` class is used to perform the classification against the bot utterances.\n",
    "\n",
    "- `targets` : an array of entities to classify.\n",
    "- `queries` : examples passed to assist the classifier\n",
    "- `model_dir` : path to the model\n",
    "\n",
    "The output should resemble the following:\n",
    "\n",
    "```bash\n",
    "Loading Inference session from C:\\Users\\<>\\models\\bidaf\\bidaf.onnx..\n",
    "Inference session loaded..\n",
    "Processed queries..\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading Inference session from C:\\Users\\daveta\\models\\bidaf\\bidaf.onnx..\n",
      "Inference session loaded..\n",
      "Processed queries..\n"
     ]
    }
   ],
   "source": [
    "s = BidafModelRuntime(\n",
    "    targets=[\"from\", \"to\", \"date\"],\n",
    "    queries={\n",
    "        \"from\": \"which city will you travel from?\",\n",
    "        \"to\": \"which city will you travel to?\",\n",
    "        \"date\": \"which date will you travel?\",\n",
    "    },\n",
    "    model_dir = bidaf_model_dir\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'from': 'london', 'to': 'paris', 'date': 'feb 14th'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.serve(\"flight to paris from london on feb 14th\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'from': 'london', 'to': 'paris', 'date': 'feb 14th'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.serve(\"book flight from london to paris on feb 14th\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'from': 'berlin', 'to': 'berlin', 'date': '5th'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.serve(\"fly from berlin to paris on may 5th\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "botsample",
   "language": "python",
   "name": "botsample"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
